
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;
import com.kugou.game.sdk.utils.RSAUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:修改（更新）支付密码
 *
 * @author jerryliu
 * @since 2015年8月25日 上午10:24:22
 */
public class UpdatePayPsdTask {

    public UpdatePayPsdResponse request(String username, String kgToken, String oldPayPassword,
            String newPayPassword) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(username);
        req.setKgToken(kgToken);
        UpdatePayPsdResponse respObj = new UpdatePayPsdResponse();
        ResponsePackage resp = new UpdatePayPsdResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();

        params.put("cmd", ProtocolCmd.CMD_UPDATE_PAY_PASSWD);
        params.put("username", username);
        params.put("kgtoken", kgToken);
        params.put("paypwd", RSAUtil.getRASEncrypt(UrlEncodeUtil.encode(oldPayPassword, "utf-8")));
        params.put("newpaypwd",
                RSAUtil.getRASEncrypt(UrlEncodeUtil.encode(newPayPassword, "utf-8")));
        requestParams.add(params);

        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    static class UpdatePayPsdResponsePackage extends HttpBaseResponsePackage<UpdatePayPsdResponse> {

        @Override
        protected void handleResponse(UpdatePayPsdResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "UpdatePayPsdTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                }
            } catch (Exception e) {
            }
        }
    }

    public static class UpdatePayPsdResponse extends ResponseMessage {
        private static final long serialVersionUID = -4820735216365527465L;
    }
}
